package com.platform.system.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.platform.common.annotation.DataScope;
import com.platform.common.utils.DateUtils;
import com.platform.system.domain.ProProduct;
import com.platform.system.mapper.ProProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.platform.system.mapper.ProCartMapper;
import com.platform.system.domain.ProCart;
import com.platform.system.service.IProCartService;

/**
 * 购物车Service业务层处理
 *
 * @author platform
 * @date 2025-02-23
 */
@Service
public class ProCartServiceImpl implements IProCartService {
    @Autowired
    private ProCartMapper proCartMapper;

    @Autowired
    private ProProductMapper proProductMapper;

    @Override
    public Map<String, Object> findAll(List<ProCart> list) {
        BigDecimal totalPrice = new BigDecimal(0);
        BigDecimal originPrice = new BigDecimal(0);
        Map<String, Object> res = new HashMap<>();

        for (ProCart cart : list) {
            Long goodsId = cart.getProducproId();
            ProProduct goods = proProductMapper.selectProProductById(goodsId);
            goods.setRealPrice(goods.getPrice().multiply(BigDecimal.valueOf(goods.getDiscount())));
            cart.setGoods(goods);
            totalPrice = totalPrice.add(goods.getRealPrice().multiply(BigDecimal.valueOf(cart.getCount())));
            originPrice = originPrice.add(goods.getPrice().multiply(BigDecimal.valueOf(cart.getCount())));
        }

        res.put("list", list);  // 购物车列表
        res.put("totalPrice", totalPrice);  // 总价
        res.put("discount", originPrice.subtract(totalPrice));    // 折扣优惠金额
        return res;
    }

    /**
     * 查询购物车
     *
     * @param id 购物车主键
     * @return 购物车
     */
    @Override
    public ProCart selectProCartById(Long id) {
        return proCartMapper.selectProCartById(id);
    }

    /**
     * 查询购物车列表
     *
     * @param proCart 购物车
     * @return 购物车
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<ProCart> selectProCartList(ProCart proCart) {
        return proCartMapper.selectProCartList(proCart);
    }

    /**
     * 查询购物车列表
     *
     * @param proCart 购物车
     * @return 购物车
     */
    @Override
    public List<ProCart> selectProCartListAll(ProCart proCart) {
        return proCartMapper.selectProCartListAll(proCart);
    }

    /**
     * 新增购物车
     *
     * @param proCart 购物车
     * @return 结果
     */
    @Override
    public int insertProCart(ProCart proCart) {
        proCart.setCreateTime(DateUtils.getNowDate());
        ProProduct proProduct = proProductMapper.selectProProductById(proCart.getProducproId());
        proCart.setProducproName(proProduct.getName());
        proCart.setProducproImg(proProduct.getImgs());
        return proCartMapper.insertProCart(proCart);
    }

    /**
     * 修改购物车
     *
     * @param proCart 购物车
     * @return 结果
     */
    @Override
    public int updateProCart(ProCart proCart) {
        proCart.setUpdateTime(DateUtils.getNowDate());
        return proCartMapper.updateProCart(proCart);
    }

    /**
     * 批量删除购物车
     *
     * @param ids 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deleteProCartByIds(Long[] ids) {
        return proCartMapper.deleteProCartByIds(ids);
    }

    /**
     * 删除购物车信息
     *
     * @param id 购物车主键
     * @return 结果
     */
    @Override
    public int deleteProCartById(Long id) {
        return proCartMapper.deleteProCartById(id);
    }
}
